/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.nxu.campus.zhxy.service.impl;

import com.nxu.campus.zhxy.domain.ZhxyBuilding;
import com.nxu.campus.utils.ValidationUtil;
import com.nxu.campus.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.nxu.campus.zhxy.repository.ZhxyBuildingRepository;
import com.nxu.campus.zhxy.service.ZhxyBuildingService;
import com.nxu.campus.zhxy.service.dto.ZhxyBuildingDto;
import com.nxu.campus.zhxy.service.dto.ZhxyBuildingQueryCriteria;
import com.nxu.campus.zhxy.service.mapstruct.ZhxyBuildingMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.nxu.campus.utils.PageUtil;
import com.nxu.campus.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author HWL
* @date 2022-04-10
**/
@Service
@RequiredArgsConstructor
public class ZhxyBuildingServiceImpl implements ZhxyBuildingService {

    private final ZhxyBuildingRepository zhxyBuildingRepository;
    private final ZhxyBuildingMapper zhxyBuildingMapper;

    @Override
    public Map<String,Object> queryAll(ZhxyBuildingQueryCriteria criteria, Pageable pageable){
        Page<ZhxyBuilding> page = zhxyBuildingRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(zhxyBuildingMapper::toDto));
    }

    @Override
    public List<ZhxyBuildingDto> queryAll(ZhxyBuildingQueryCriteria criteria){
        return zhxyBuildingMapper.toDto(zhxyBuildingRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ZhxyBuildingDto findById(Long id) {
        ZhxyBuilding zhxyBuilding = zhxyBuildingRepository.findById(id).orElseGet(ZhxyBuilding::new);
        ValidationUtil.isNull(zhxyBuilding.getId(),"ZhxyBuilding","id",id);
        return zhxyBuildingMapper.toDto(zhxyBuilding);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ZhxyBuildingDto create(ZhxyBuilding resources) {
        return zhxyBuildingMapper.toDto(zhxyBuildingRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ZhxyBuilding resources) {
        ZhxyBuilding zhxyBuilding = zhxyBuildingRepository.findById(resources.getId()).orElseGet(ZhxyBuilding::new);
        ValidationUtil.isNull( zhxyBuilding.getId(),"ZhxyBuilding","id",resources.getId());
        zhxyBuilding.copy(resources);
        zhxyBuildingRepository.save(zhxyBuilding);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            zhxyBuildingRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ZhxyBuildingDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ZhxyBuildingDto zhxyBuilding : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("楼宇名称", zhxyBuilding.getBuildingName());
            map.put("类型", zhxyBuilding.getBuildingType());
            map.put("状态", zhxyBuilding.getBuildingStatus());
            map.put("描述", zhxyBuilding.getBuildingDesc());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
